﻿using CKS.Core.Framework;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZR.Model.Enum;
using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription.OP20
{
    /*
     *OP20 盖板检测数据
     */
    public class DB3041_Work04Action : PLCAction
    {
        /// <summary>
        /// 停用，已经采用 数据采集通用保存
        /// </summary>
        /// <param name="_value"></param>
        public DB3041_Work04Action(DecodeValue _value) : base(_value)
        {
        }
        public override void Execute()
        {
            var result = Value.Value.ToInt();
            string batchNo = GetPartBatchNo((int)PartTypeEnum.定位柱小焊环);
            if (result == 1)
            {

                var coverResult =Value.GetLinkData(99).ToInt(); //1 ok 2 ng

                string CoverCode = Value.GetLinkData(1).ToString();
                DcCoverrivetingassemble dc = null;
                //dc = db.Queryable<DcCoverrivetingassemble>().Where(a => a.CoverCode == CoverCode).First();

                bool add = false;
                if(dc == null)
                {
                    dc = new DcCoverrivetingassemble();
                    add = true;
                }

                dc.SmallSolderRingBatchCode = batchNo;
                dc.CoverCode = CoverCode;
                dc.CoverBlueCode = Value.GetLinkData(16).ToString();
                dc.CCD_2D1 = Value.GetLinkData(2).ToInt();
                dc.CCD_2D2 = Value.GetLinkData(3).ToInt();
                dc.CCD_3D = Value.GetLinkData(4).ToInt();
                

                dc.HoleDiameter = Value.GetLinkData(5).ToDecimal();
                dc.SlotWidth = Value.GetLinkData(6).ToDecimal();
                dc.SlotLenUpper = Value.GetLinkData(7).ToDecimal();
                dc.SlotLenLower = Value.GetLinkData(8).ToDecimal();
                dc.SlotWidthUpper = Value.GetLinkData(9).ToDecimal();
                dc.SlotWidthLower = Value.GetLinkData(10).ToDecimal();
                dc.Flatness = Value.GetLinkData(11).ToDecimal();
                dc.HeightMax = Value.GetLinkData(12).ToDecimal();
                dc.HeightMin = Value.GetLinkData(13).ToDecimal();
                dc.Profile = Value.GetLinkData(14).ToDecimal();
                dc.PositionDegree = Value.GetLinkData(15).ToDecimal();
                dc.CoverResult = Value.GetLinkData(99).ToInt();
                dc.AssembleTime = DateTime.Now;

                if (coverResult == 2) //只保存NG结果。总结果会存储检测通过的
                {
                    dc.TotalResult = 2;
                    db.Insertable(dc).ExecuteCommand();
                    EventBus.AddLog($"OP20盖板检测-NG-{CoverCode}保存操作");
                }
                else
                {
                    EventBus.AddLog($"OP20盖板检测-OK-{CoverCode}保存信号");
                }


                // db.Insertable(dc).ExecuteCommand();

                //保存数据
                //读取子篮二维码和检测结果
                //string barcode = Value.GetLinkData(1).ToString();
                //int flag = Value.GetLinkData(2).ToInt();

                //logger.LogInformation($"保存数据 子篮码{barcode} 检测结果{flag}");
                //查找保存完成点位
                //EventBus.PLCUpdate("OP20请求保存数据");
                var replayPos = Value.GetPosition(3050, 4);
                AckWriteInt16(replayPos, 1);
                
            }
            else
            {
                var replayPos = Value.GetPosition(3050, 4);
                AckWriteInt16(replayPos, 0);
                EventBus.AddLog("OP20盖板检测-重置保存状态");
            }
        }
    }
}
